# -*- coding: utf-8 -*-
# @Time    : 2022/6/29 16:53
# @Author  : AUGUS
# @Email   : augus@jilin.com
# @File    : serializers.py
# @Software: PyCharm
from django.contrib.auth.models import User
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

from users.models import UserDetail


class UserDetailSerializer(ModelSerializer):
    class Meta:
        model = UserDetail
        exclude = ['is_delete']

        extra_kwargs = {
            'avatar': {'read_only': True},
            'user': {'required': False, 'write_only': True}
        }


class UserSerializer(ModelSerializer):
    userdetail = UserDetailSerializer(required=False, read_only=True)

    class Meta:
        model = User
        exclude = ['password', 'last_name', 'user_permissions']

        extra_kwargs = {
            'id': {'read_only': True},
            'last_login': {'read_only': True},
            'is_superuser': {'read_only': True},
            'is_staff': {'read_only': True},
            'is_active': {'read_only': True},
            'date_joined': {'read_only': True},
            'groups': {'read_only': True},
        }


class UpdatePasswordSerializer(serializers.Serializer):
    password = serializers.CharField(max_length=60, label='密码')
    new_password = serializers.CharField(max_length=60, label='新密码')
    re_new_password = serializers.CharField(max_length=60, label='确认密码')

    def validate(self, attrs):
        if attrs['new_password'] != attrs['re_new_password']:
            raise serializers.ValidationError('两次密码输入不一致')
        return attrs
